// pages/my/my-dealDetail/my-dealDetail.js
var request = require('../../../utils/request.js');
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    showTip: true
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    that.getToken();
  },

  getToken: function () {
    var that = this;
    // 获取本地存储中的token
    wx.getStorage({
      key: 'token',
      success: function (res) {//有该数据
        console.log(res.data)
        that.setData({
          token: res.data
        })
        var tempToken = res.data;
        request.getRecord(
          { "session_id": tempToken },
          (res) => {
            console.log(res);
            if (res.data.code == "1" && res.data.data.length > 0) {
              that.setData({
                list: dealDate(res.data.data),
                showTip: false
              })
              console.log(dealDate(res.data.data))
              
            } else {
              that.setData({
                showTip: true
              })
            }

          }
        )

      },
      fail: function (res) {
        console.log(res.data)
        //调用授权方法-设置token
        app.getUserInfo();
      }
    })
  },
  onShow: function () {
  
  },


})

//处理重复数据显示
function dealDate(arr){
  var obj = {}
  for (var i = 0; i < arr.length; i++) {
    var index = arr[i].time1.slice(0,7);
    if (obj[index]) {
      obj[index].push(arr[i])
    } else {
      obj[index] = [];
      obj[index].push(arr[i])
    }
  }
console.log(obj)
  var tempArr = [];
  for (var i in obj) {
    var tempObj = {};
    tempObj.month = i.split("-").join("年")+"月";
    tempObj.monthlist = obj[i];
    tempArr.push(tempObj)
  }
  return tempArr;

}